var sError = "'delete on table " + sToTable + " violates foreign key constraint'";
aQ.push('DROP TRIGGER IF EXISTS ' + delTrigName);
if (sOnDelete.toUpperCase() == "CASCADE") {
aQ.push('CREATE TRIGGER IF NOT EXISTS ' + delTrigName + ' BEFORE DELETE ON "' + sToTable + '" FOR EACH ROW BEGIN DELETE FROM "' + sTableName + '" WHERE ' + sWhereFrom + '; END;');
}
else {
aQ.push('CREATE TRIGGER IF NOT EXISTS ' + delTrigName + ' BEFORE DELETE ON "' + sToTable + '" FOR EACH ROW BEGIN SELECT RAISE(ROLLBACK, ' + sError + ') WHERE (SELECT ' + sSelectColsFrom + ' FROM "' + sTableName + '" WHERE ' + sWhereFrom + ') IS NOT NULL; END;');
var sError = "'update on table " + sToTable + " violates foreign key constraint'";
aQ.push('DROP TRIGGER IF EXISTS ' + updParTrigName);
if (sOnUpdate.toUpperCase() == "CASCADE") {//after update
aQ.push('CREATE TRIGGER IF NOT EXISTS ' + updParTrigName + ' AFTER UPDATE ON "' + sToTable + '" FOR EACH ROW BEGIN UPDATE "' + sTableName + '" SET ' + sSetClause + " WHERE " + sWhereFrom + '; END;');
}
else {
aQ.push('CREATE TRIGGER IF NOT EXISTS ' + updParTrigName + ' BEFORE UPDATE ON "' + sToTable + '" FOR EACH ROW BEGIN SELECT RAISE(ROLLBACK, ' + sError + ') WHERE (SELECT ' + sSelectColsFrom + ' FROM "' + sTableName + '" WHERE ' + sWhereFrom + ') IS NOT NULL; END;');
}
}
if (sm_confirm(sm_getLStr("sqlm.confirm.title"), sm_getLStr("sqlm.confirm.msg") + aQ.join('\n\n'))) {
//Issue #149: must connect in exclusive mode to connect to the actual file rather than the cached file; correspondingly, make exclusive mode the default one.
//TODO: sort this funny issue!! the assignment below succeeds but when I select a master table, the border of the tree encloses only so many rows as were visible in the previously shown table.
//TODO: set a user-pref for max/min rows or a splitter?
//loadTabBrowse: populates the table list and the tree view for current table; must be called whenever a database is opened/closed and whenever the schema changes; depends entirely upon the values in "browse-type" and "browse-name" controls
loadTabBrowse: function() {
//no need to waste resources if this tab is not selected
if(this.getSelectedTabId() != "tab-browse")
return false;
if (this.sCurrentDatabase == null)
return false;
if (this.mostCurrObjType == null)
return false;
var sObjType = this.mostCurrObjType.toLowerCase();
// var bConfirm = sm_confirm(sm_getLStr("dangerous.op"), "This is a potentially dangerous operation. SQLite does not support statements that can alter a column in a table. Here, we attempt to reconstruct the new CREATE SQL statement by looking at the pragma table_info which does not contain complete information about the structure of the existing table.\n\n" + sm_getLStr("q.proceed"));
// if (!bConfirm)
// return false;
var treeCol = $$("treeTabCols");
var row = treeCol.view.selection.currentIndex;
var col = treeCol.columns.getColumnAt(1);
var sOldName = treeCol.view.getCellText(row, col);
var col = treeCol.columns.getColumnAt(2);
var sOldType = treeCol.view.getCellText(row, col);
var col = treeCol.columns.getColumnAt(4);
var sOldDefault = treeCol.view.getCellText(row, col);
var sTable = treeCol.getAttribute("smTableName");
$$("tb-ec-table").value = sTable;
$$("tb-ec-oldName").value = sOldName;
$$("tb-ec-oldType").value = sOldType;
$$("tb-ec-oldDefault").value = sOldDefault;
$$("tb-ec-newName").value = sOldName;
$$("tb-ec-newType").value = sOldType;
$$("tb-ec-newDefault").value = sOldDefault;
$$("gb-editColumn").hidden = false;
$$("tb-ec-newName").focus();
},
alterColumn: function() {
var bConfirm = sm_confirm(sm_getLStr("dangerous.op"), sm_getLStr("sqlm.confirm.dangerousOp") + sm_getLStr("q.proceed"));
if (!bConfirm)
return false;
var sTable = $$("tb-ec-table").value;
var sOldName = $$("tb-ec-oldName").value;
var sNewName = $$("tb-ec-newName").value;
if (sNewName.length == 0) {
alert(sm_getLStr("sqlm.alterColumn.name"));
return false;
}
var sNewType = $$("tb-ec-newType").value;
var sNewDefVal = $$("tb-ec-newDefault").value;
if (sNewDefVal.length == 0)
sNewDefVal = null;
var aNewInfo = {oldColName: sOldName,
newColName: sNewName,
newColType: sNewType,
newDefaultValue: sNewDefVal};
var bReturn = CreateManager.modifyTable("alterColumn", sTable, aNewInfo);
if(bReturn) {
this.cancelEditColumn();
this.refreshDbStructure();
this.loadTabBrowse();
}
return bReturn;
},
dropColumn: function() {
var bConfirm = sm_confirm(sm_getLStr("dangerous.op"), sm_getLStr("sqlm.confirm.dangerousOp") + sm_getLStr("q.proceed"));
if (!bConfirm)
return false;
// var bConfirm = sm_prefsBranch.getBoolPref("allowUnsafeTableAlteration");
var treeCol = $$("treeTabCols");
var row = treeCol.view.selection.currentIndex;
var col = treeCol.columns.getColumnAt(1);
var sColumn = treeCol.view.getCellText(row, col);
var sTable = treeCol.getAttribute("smTableName");
var bReturn = CreateManager.modifyTable("dropColumn", sTable, sColumn);